package site.zhouinfo.ip;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * ip
 */
public class IPUtil {

    /**
     * @param ip ping ip
     * @return 是否ping通
     */
    public static boolean isReachable(String ip) {
        boolean reachable = false;
        reachable = ping(ip).contains("TTL");
        return reachable;
    }

    /**
     * @param ip ping ip
     * @return 结果
     */
    public static String ping(String ip) {
        try {
            Process p = Runtime.getRuntime().exec("ping -n 1 " + ip);
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                if (line.length() != 0)
                    sb.append(line + "\r\n");
            }
            br.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
